clear all
*cd "I:\workdata\706727\Build\Data"
cd "J:\workdata\706727\Temp"

use psychighfreq_allages

keep if inrange(year,1995,2014)

compress

save psychighfreq_allages, replace

sort pnr year month
cap drop num
by pnr: gen num=_n

cap drop Dyear
gen byte Dyear=1 if (year==2008 & month>3) | inrange(year,2009,2014) 
replace Dyear=0 if (year==2008 & month<4) | year<2008

gen yrm=year+(month-1)/12

cap drop DepSev
gen byte DepSev=1 if     (bamb_1==320 | bamb_2==320 | bamb_1==330 | bamb_2==330  | erp_1==320 | erp_2==320 | erp_1==330 | erp_2==330 | st_phosp_1==320 | st_phosp_2==320 | st_phosp_1==330 | st_phosp_2==330)
replace DepSev=2 if (bamb_1==321 | bamb_2==321 | bamb_1==331 | bamb_2==331  | erp_1==321 | erp_2==321 | erp_1==331 | erp_2==331 | st_phosp_1==321 | st_phosp_2==321 | st_phosp_1==331 | st_phosp_2==331)
replace DepSev=3 if (bamb_1==322 | bamb_2==322 | bamb_1==332 | bamb_2==332  | erp_1==322 | erp_2==322 | erp_1==332 | erp_2==332 | st_phosp_1==322 | st_phosp_2==322 | st_phosp_1==332 | st_phosp_2==332 | ///
					 bamb_1==323 | bamb_2==323 | bamb_1==333 | bamb_2==333  | erp_1==323 | erp_2==323 | erp_1==333 | erp_2==333 | st_phosp_1==323 | st_phosp_2==323 | st_phosp_1==333 | st_phosp_2==333)
replace DepSev=0 if (floor(bamb_1/10)==32 | floor(bamb_1/10)==33 | floor(bamb_2/10)==32 | floor(bamb_2/10)==33 | ///
					floor(erp_1/10)==32 | floor(erp_1/10)==33 | floor(erp_2/10)==32 | floor(erp_2/10)==33 | ///
					floor(st_phosp_1/10)==32 | floor(st_phosp_1/10)==33 | floor(st_phosp_2/10)==32 | floor(st_phosp_2/10)==33) & mi(DepSev)
					
foreach var in bamb_1 bamb_2 erp_1 erp_2 st_phosp_1 st_phosp_2 {
gen r`var'=floor(`var'/100) if `var'!=999
replace r`var'=10 if `var'==999
replace `var'=r`var'
drop r`var'
}

gen byte Dsui=((asui>0 & !mi(asui)) | c_liste49=="48"/* | psui==1*/)
gen byte Dbamb_AnxDep=(bamb_1==3 | bamb_2==3 | bamb_1==4 | bamb_2==4)
gen byte Derp_AnxDep=(erp_1==3 | erp_2==3 | erp_1==4 | erp_2==4)
gen byte DPHosp_AnxDep=(st_phosp_1==3 | st_phosp_2==3 | st_phosp_1==4 | st_phosp_2==4)

gen byte Dpsykhosp=(!mi(bamb_1) | !mi(erp_1) | !mi(st_phosp_1))

foreach var in mentaldiag psyc psyk_test psychiatry therapy pois pois_painkiller psykmed adep anxiety sleep ssri psychostim antipsych {

replace `var'=0 if mi(`var')

cap drop D`var'
gen byte D`var'=(`var'>0 & !mi(`var'))
}
/* One-year first */
foreach var in mentaldiag psyc psykhosp psyk_test psychiatry therapy sui pois pois_painkiller   {
sort pnr D`var' year month
cap drop first`var'
by pnr: gen byte first`var'=((D`var'==1 & D`var'[_n-1]==1 & yrm-yrm[_n-1]>=1) | (D`var'==1 & D`var'[_n-1]==0)) if pnr[_n-1]==pnr 
}
/* Five-year first */
sort pnr Dpsykmed year month
cap drop firstpsykmed
by pnr: gen byte firstpsykmed=((Dpsykmed==1 & Dpsykmed[_n-1]==1 & yrm-yrm[_n-1]>=5) | (Dpsykmed==1 & Dpsykmed[_n-1]==0)) if pnr[_n-1]==pnr
gen Danxsleep=(Danxiety==1 | Dsleep==1) 
foreach var in adep anxiety sleep anxsleep ssri psychostim antipsych {
gen byte first`var'=(D`var'==1 & firstpsykmed==1)
}
keep if inrange(year,1999,2014)

/* Restrict to individuals in the data one year before */
sort pnr year month

cap drop keep
by pnr: gen byte keep=1 if     (pnr==pnr[_n-1] & num==num[_n-1]+1) & ///
			(pnr==pnr[_n-2] & num==num[_n-2]+2) & ///
			(pnr==pnr[_n-3] & num==num[_n-3]+3) & ///
			(pnr==pnr[_n-4] & num==num[_n-4]+4) & ///
			(pnr==pnr[_n-5] & num==num[_n-5]+5) & ///
			(pnr==pnr[_n-6] & num==num[_n-6]+6) & ///
			(pnr==pnr[_n-7] & num==num[_n-7]+7) & ///
			(pnr==pnr[_n-8] & num==num[_n-8]+8) & ///
			(pnr==pnr[_n-9] & num==num[_n-9]+9) & ///
			(pnr==pnr[_n-10] & num==num[_n-10]+10) & ///
			(pnr==pnr[_n-11] & num==num[_n-11]+11) & ///
			(pnr==pnr[_n-12] & num==num[_n-12]+12) 
			
keep if inrange(year,2000,2014)
keep if keep==1

drop num

cap drop age
gen age=year-birthyear  if birthmonth<=month & !mi(month)
replace age=year-birthyear-1 if birthmonth>month & !mi(birthmonth)

cap drop monthdist
gen monthdist=month-birthmonth
replace monthdist=monthdist+12 if monthdist<0 

cap drop agec
gen agec=age+monthdist/12

compress

duplicates drop pnr year month, force

cd "I:\workdata\706727\Build\Data"

save psychighfreq_AllAges_finalfull, replace

keep pnr year month koen  agec kom  birthmonth birthyear erp_* st_phosp_* bamb_* Dpsyc Dtherapy Dpsychiatry  Dpsykhosp   ///
	DPHosp_AnxDep Derp_AnxDep Dbamb_AnxDep Dsui   ///
	Dpsykmed Danxiety Dsleep Danxsleep Dadep Dssri Dpsychostim Dantipsych  Dpois Dpois_painkiller  first*  

save psychighfreq_AllAges_final, replace



